const { findText } = require('./foo.js');
window = floaty.rawWindow(
    <frame margin="0 0 0 0" gravity="left|center">
        <frame id="drawer" margin="0 0 0 0" h="{{xf_h}}" w="{{xf_w}}" bg="#a3b5c7" visibility="gone">
            <img id="img1" src="file://icon/log.png" circle="true" w="26" h="26" alpha="0.8" />
            <img id="img2" src="file://icon/pause.png" gravity="left|center" circle="true" w="26" h="26" alpha="0.8" />
            <img id="img3" src="file://icon/start.png" gravity="left|center" circle="true" w="26" h="26" alpha="0.8" />
        </frame>
        <img
            bg="#000000"
            id="ui_controller"
            src="file://icon/star.png"
            layout_gravity="left"
            marginTop="{{xf_mg}}"
            w="30"
            h="30"
            alpha="0.6"
        />
    </frame>
);

//判断是否具有悬浮窗权限
if (!floaty.checkPermission()) {
    // 没有悬浮窗权限，提示用户并跳转请求
    toast('本脚本需要悬浮窗权限来显示悬浮窗，请在随后的界面中允许并重新运行本脚本。');
    floaty.requestPermission();
    exit();
} else {
    toastLog('已有悬浮窗权限');
}

//设置控件初始位置
sleep(100);
var wid1 = window.ui_controller.getWidth() / 2;
var wid2 = device.height / 2;
window.setPosition(wid2 - wid1, 0);
//悬浮窗关闭脚本停止
window.exitOnClose();

//记录按键被按下时的触摸坐标
var flag_wzsb = false;
var x = 0,
    y = 0;
//记录按键被按下时的悬浮窗位置
var windowX, windowY;
//记录按键被按下的时间以便判断长按等动作
var downTime;
//设置悬浮按钮可拖动，判断长按\点击
window.ui_controller.setOnTouchListener(function (view, event) {
    switch (event.getAction()) {
        case event.ACTION_DOWN:
            x = event.getRawX();
            y = event.getRawY();
            windowX = window.getX();
            windowY = window.getY();
            downTime = new Date().getTime();
            return true;
        case event.ACTION_MOVE:
            //移动手指时调整悬浮窗位置
            wX = windowX + (event.getRawX() - x);
            wY = windowY + (event.getRawY() - y);

            wX = wX > -wid1 ? wX : -wid1;
            window.setPosition(wX, wY);
            return true;
        case event.ACTION_UP:
            //如果按下的时间超过0.5秒判断为长按，退出脚本
            if (
                new Date().getTime() - downTime > 500 &&
                Math.abs(event.getRawY() - y) < 5 &&
                Math.abs(event.getRawX() - x) < 5
            ) {
                ui.finish();
            }
            //手指弹起时如果偏移很小则判断为点击
            if (
                Math.abs(event.getRawY() - y) < 5 &&
                Math.abs(event.getRawX() - x) < 5 &&
                new Date().getTime() - downTime <= 800
            ) {
                function onClick() {
                    if (!flag) {
                        kai();
                    } else {
                        guan();
                        flag = false;
                    }
                }
                onClick();
            }

            if (flag_wzsb) {
                threads.start(() => {
                    flag_wzsb = false;
                    result_arr = findText(null, [0, 0, device.height, device.width]);
                    if (result_arr) {
                        result_arr.forEach(e => {
                            setClip(e.text);
                        });
                        toast('已设置剪贴板内容');
                    } else {
                        toast('没有可转换内容');
                    }
                });
            }
            return true;
    }
    return true;
});

//浮窗主按钮点击动作
var alpha0 = 0;
var alpha1 = 0.8;
var x1 = 0,
    y1 = 0; //从上至下第1个按钮起始位置
var x2 = 0,
    y2 = 0; //从上至下第2个按钮起始位置
var x3 = 0,
    y3 = 0; //从上至下第3个按钮起始位置

var w_device = device.width;
var a = 0.4 * (w_device / 720); //各图标之间距离系数
flag = false; //副功能按钮为收拢状态
function kai() {
    for (i = 0; i < 100; i++) {
        setTimeout(() => {
            if (storage.get('configure').xfan1) {
                x1 += 2 * a;
                y1 -= 0;
                x2 += 3.5 * a;
                x3 += 5 * a;
                y3 += 0;
            } else {
                x1 += 1.2 * a;
                y1 -= 1.5 * a;
                x2 += 2.5 * a;
                x3 += 1.2 * a;
                y3 += 1.5 * a;
            }

            window.img1.translationX = x1;
            window.img1.translationY = y1;

            window.img2.translationX = x2;

            window.img3.translationX = x3;
            window.img3.translationY = y3;

            alpha0 += 0.006;
            alpha1 -= 0.006;

            window.drawer.alpha = alpha0;
            window.ui_controller.alpha = alpha1;
        }, i * 2);
    }
    window.drawer.visibility = 0;
    flag = true;
} //副功能按钮展开动作
function guan() {
    for (i = 0; i < 100; i++) {
        setTimeout(() => {
            if (storage.get('configure').xfan1) {
                x1 -= 2 * a;
                y1 += 0;
                x2 -= 3.5 * a;
                x3 -= 5 * a;
                y3 -= 0;
            } else {
                x1 -= 1.2 * a;
                y1 += 1.5 * a;
                x2 -= 2.5 * a;
                x3 -= 1.2 * a;
                y3 -= 1.5 * a;
            }

            window.img1.translationX = x1;
            window.img1.translationY = y1;

            window.img2.translationX = x2;

            window.img3.translationX = x3;
            window.img3.translationY = y3;

            alpha0 -= 0.006;
            alpha1 += 0.006;

            window.drawer.alpha = alpha0;
            window.ui_controller.alpha = alpha1;
        }, i * 2);
    }
    setTimeout(function () {
        ui.run(() => {
            window.drawer.visibility = 8;
        });
    }, 200);
}

//副功能键功能定义
var w1 = null; //暂停遮罩层
var isShow = false;
window.img1.on('click', () => {
    //副功能一：日志显示与隐藏
    threads.start(function () {
        isShow ? console.hide() : console.show();
        isShow = !isShow;
        //点击副功能键后收容，并恢复标志
        guan();
        flag = false;
    });
});
window.img2.on('click', () => {
    //副功能二：脚本暂停与启动
    pause_flag = require('./foo.js').p_r.pause_flag;
    setPauseFlag = require('./foo.js').p_r.setPauseFlag;
    setPauseFlag();
    if (pause_flag) {
        w1 ? w1.close() : null;
        toastLog('脚本已继续');
    } else {
        toastLog('脚本已暂停');
        threads.start(function () {
            w1 = floaty.rawWindow(
                <vertical gravity="center" bg="#44ffcc00">
                    <text
                        id="zt"
                        text="Pausing..."
                        w="*"
                        layout_weight="1"
                        textColor="red"
                        textSize="40sp"
                        gravity="bottom|center"
                    />
                    <text
                        text="（点击悬浮按钮-->功能二按钮继续）"
                        w="auto"
                        layout_weight="1"
                        textStyle="bold"
                        gravity="top|center"
                    />
                </vertical>
            );

            w1.setSize(-1, -1);
            w1.setTouchable(false);
        });
    }
    //点击副功能键后收容，并恢复标志
    guan();
    flag = false;
});
window.img3.on('click', (eve, vie) => {
    flag_wzsb = true;

    ui.setContentView(layout1);
    //点击副功能键后收容，并恢复标志
    guan();
    flag = false;
});

exports.window = window;
